import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
import resolve from "@rollup/plugin-node-resolve";

// https://vitejs.dev/config/
export default defineConfig({
  define: {
    "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
  },
  plugins: [
    vue(),
    resolve(),
    cssInjectedByJsPlugin(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  build: {
    lib: {
      entry: "./src/MyPlugin.js",
      name: "MyPlugin",
      fileName: (format) => `MyPlugin.${format}.js`, // 打包后的文件名【可以自定义】
      formats: ["umd"],
    },
    rollupOptions: {
      output: {
        // chunkFileNames: `chat/[name]-[hash].js`,
        // entryFileNames: `chat/[name]-[hash].js`,
        // assetFileNames: () => {
        //   return `chat/[name]-[hash].[ext]`;
        // },
        external: ["vue", "MarkdownIt"],
        globals: {
          vue: "Vue",
          MarkdownIt: "MarkdownIt",
        },
      },
    },
  },
  server: {
    port: 5173,
    open: false,
    // host: "127.0.0.1",
    proxy: {
      "/api": {
        // target: "http://47.100.201.252:8001/",
        // target: "http://agent.ccic-test.com.cn/",
        target: "http://localhost:3000/",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
});
